package com.harunasan.carrental.api.serve.service.impl;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harunasan.carrental.api.serve.dao.StoreMapper;
import com.harunasan.carrental.api.serve.domain.entity.Store;
import com.harunasan.carrental.api.serve.domain.post.StorePost;
import com.harunasan.carrental.api.serve.service.IStoreService;
import com.harunasan.carrental.common.exception.Assert;
import com.harunasan.carrental.common.exception.OperationException;
import com.harunasan.carrental.common.query.CommonQueryParam;
import com.harunasan.carrental.common.utils.CommonQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:06:16
 */
@Service
@Slf4j
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    /**
     * 获取分页
     *
     * @param commonQueryParam 查询参数
     * @return 分页
     */
    @Override
    public IPage<Store> pageList(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
                new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
                new CommonQueryWrapper<Store>(commonQueryParam).lambda()
                        .eq(Store::getIsDeleted, false)
        );
    }

    /**
     * 为门店分配管理员
     *
     * @param storeId 门店标识
     * @param adminId 管理员标识
     * @return x
     */
    @Override
    public Boolean storeManagerAdd(Long storeId, Long adminId) {
        Store store=this.getOne(
                new QueryWrapper<Store>().lambda()
                        .eq(Store::getStoreId,storeId)
                        .eq(Store::getIsDeleted,false)
        );
        Assert.notNull(store,"无此门店信息！");
        store.setManagerId(adminId);
        if(!this.updateById(store)){
            throw new OperationException("分配门店管理员失败！");
        }
        return true;
    }

    /**
     * 添加门店
     *
     * @param storePost 门店参数
     * @return 是否成功
     */
    @Override
    public Boolean create(StorePost storePost) {
        Store store=new Store();
        BeanUtils.copyProperties(storePost,store);
        if (!this.save(store)) {
            throw new OperationException("添加门店失败！");
        }
        return true;
    }

    /**
     * 逻辑删除
     *
     * @param id id
     * @return 是否成功
     */
    @Override
    public Boolean delete(Long id) {
        //先查询，再更新
        Store store = this.getById(id);
        Assert.notNull(store, "删除失败，无此门店！");
        store.setIsDeleted(true);
        if (!this.updateById(store)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 修改门店信息
     * @param id id
     * @param storePost 门店参数
     * @return 是否成功
     */
    @Override
    public Boolean update(Long id,StorePost storePost) {
        //先查询，再更新
        Store store = this.getById(id);
        Assert.notNull(store, "修改失败，无此门店！");
        System.out.println(store.getStoreId());
        BeanUtils.copyProperties(storePost, store,"storeId");
        System.out.println(store.getStoreId());
        if (!this.updateById(store)) {
            throw new OperationException("添加门店信息失败！");
        }
        return true;
    }

    /**
     * 查看门店信息
     *
     * @param id 门店参数
     * @return 门店信息
     */
    @Override
    public Store detail(Long id) {
        Store store=this.getById(id);
        Assert.notNull(store,"查询失败，无此门店");
        return store;
    }

    /**
     * 获取列表
     *
     * @param commonQueryParam 查询参数
     * @return 列表
     */
    @Override
    public List<Store> list(CommonQueryParam commonQueryParam) {
        return this.list(
                new CommonQueryWrapper<Store>(commonQueryParam).lambda()
                        .eq(Store::getIsDeleted, false)
        );
    }

}
